package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.OrderAddBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.OrderService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AcEnterActivity extends AppCompatActivity {
    private ImageButton up;
    private ImageButton low;
    private TextView goodsnum;

    private int aid;
    private int uid;
    private int number;
    private String payway;


    private int cost;
    private String name;
    private TextView acname;
    private TextView price;
    private RadioGroup radioGroup;
    private RadioButton wechatButton;
    private RadioButton alipayButton;
    private TextView sum_money;
    private Button button;
    private int oid;

    private ImageButton back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acenter);

        aid = getIntent().getIntExtra("aid",-1);
        cost = getIntent().getIntExtra("cost",-1);
        name = getIntent().getStringExtra("name");

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        uid = sharedPreferences.getInt("user_id",0);

        acname = (TextView) findViewById(R.id.acenter_name);
        acname.setText(name);

        price = (TextView) findViewById(R.id.acenter_price);
        String p = String.valueOf(cost);
        price.setText(p + "元/人");


        sum_money = (TextView) findViewById(R.id.sum_money);
        goodsnum = (TextView) findViewById(R.id.textView41);
        up = (ImageButton) findViewById(R.id.imageButton6);
        low = (ImageButton) findViewById(R.id.imageButton);

        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goodsnum.setText(Integer.valueOf(goodsnum.getText().toString()) + 1 + "");
                String num = goodsnum.getText().toString().trim();
                number = Integer.parseInt(num);
                int sumgoods = number * cost;
                String i = String.valueOf(sumgoods);
                sum_money.setText("共计:   " + i + "元");
                return;
            }
        });

        low.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (goodsnum.getText().toString().equals("1")){
                    goodsnum.setText(1+ "");
                }else {
                    goodsnum.setText(Integer.valueOf(goodsnum.getText().toString()) - 1 + "" );
                    String num = goodsnum.getText().toString().trim();
                    number = Integer.parseInt(num);
                    int sumgoods = number * cost;
                    String i = String.valueOf(sumgoods);
                    sum_money.setText("共计:   " + i + "元");

                }
                return;
            }
        });


        wechatButton = (RadioButton) findViewById(R.id.radioButton15);
        alipayButton = (RadioButton) findViewById(R.id.radioButton14);
        radioGroup = (RadioGroup) findViewById(R.id.pay_radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (wechatButton.getId() == checkedId){
                    payway = wechatButton.getText().toString();
                }
                if (alipayButton.getId() == checkedId){
                    payway = alipayButton.getText().toString();
                }
                Toast.makeText(getApplicationContext(),"当前选择的付款方式为:"+payway,Toast.LENGTH_LONG).show();
            }
        });
        radioGroup.check(R.id.radioButton15);

        button = (Button) findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(Const.BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                OrderService service = retrofit.create(OrderService.class);
                Call<OrderAddBean> call = service.orderadd(uid,aid,number,payway);
                call.enqueue(new Callback<OrderAddBean>() {
                    @Override
                    public void onResponse(Call<OrderAddBean> call, Response<OrderAddBean> response) {
                        oid = response.body().getOid();
                        Intent intent = new Intent(AcEnterActivity.this,OrderDetailActivity.class);
                        intent.putExtra("oid",oid);
                        startActivity(intent);
                    }

                    @Override
                    public void onFailure(Call<OrderAddBean> call, Throwable t) {

                    }
                });
            }
        });

        back = (ImageButton) findViewById(R.id.acenter_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });




    }


}


